<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:include page="../../base.jsp"></jsp:include>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<script>
	$(function(){
		loadList();
		$('#userList').datagrid('resize', {
			width:1000,
			height:500
		});
	});
	
	//获取被选中用户的ID
	function getSelections() {
		var ids = [];
		var rows = $('#userList').datagrid('getSelections');
		for(var i=0;i<rows.length;i++){
			ids.push(rows[i].id);
		}
		return ids;
	}
	
	//删除用户
	function delUser(ids) {
		$.ajax({
			   type: "POST",
			   url: "/S3H3L3/userInfo/delUser.do?ids=" + ids,
			   success: function(data){
				   closeDeleteDialog();
				   $('#userList').datagrid('clearSelections');
				   $('#userList').datagrid('reload');
			   },
			   error:function(){
				      alert("删除失败!");
				     }
			});
	}
	
	//加载列表
	function loadList() {
		$('#userList').datagrid({
			title:'用户列表',
			nowrap: false,
			striped: true,
			collapsible:true,
			url:'/S3H3L3/userInfo/getAllUserList.do',
			sortName: 'id',
			sortOrder: 'desc',
			remoteSort: false,
			idField:'id',
			columns:[[
				{field:'ck',checkbox:true},
				{field:'userName',title:'用户名',width:200},
				{field:'sex',title:'性别',width:50,
					formatter:function(value,rec){
						if (value == 1) {
							return '男';
						} else {
							return '女';
						}
					}},
				{field:'age',title:'年龄',width:50},
				{field:'address',title:'地址',width:550}
			]],
			pagination:true,
			rownumbers:true,
			toolbar:[{
				id:'btnadd',
				text:'添加',
				iconCls:'icon-add',
				handler:function(){
					$('#btnsave').linkbutton('enable');
					alert('add');
				}
			},{
				id:'btncut',
				text:'删除',
				iconCls:'icon-cut',
				handler:function(){
					$('#btnsave').linkbutton('enable');
					openDeleteDialog();
				}
			},'-',{
				id:'btnsave',
				text:'编辑',
				iconCls:'icon-save',
				handler:function(){
					$('#btnsave').linkbutton('enable');
					openEditWindow();
				}
			}]
		});
	}
	
	//打开删除对话框
	function openDeleteDialog() {
		//删除对话框初始化
		$('#deleteDialog').dialog({
			buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					delUser(getSelections());
				}
			},{
				text:'取消',
				handler:function() {
					$('#deleteDialog').dialog('close');
				}
			}]
		});
		$('#deleteDialog').dialog('open');
	}
	
	//关闭删除对话框
	function closeDeleteDialog() {
		$('#deleteDialog').dialog('close');
	}
	
	//打开编辑对话框
	function openEditWindow() {
		var ids = getSelections();
		if (ids.length != 1) {
			alert('编辑只能选择一条记录！');
			return;
		}
		$('#editIframe').attr('src', "/S3H3L3/userInfo/getUserInfoById.do?id=" + ids[0]);
		$('#userEditWindow').window("setTitle", "编辑").window("open");
		$('#userList').datagrid('clearSelections');
	}
	
	//关闭编辑对话框
	function closeEditWindow() {
		$('#userEditWindow').window('close');
		$('#userList').datagrid('reload');
	}
	
	function refreshList() {
		$('#userList').datagrid('reload');
	}
</script>
</head>
<body>
<table id="userList"></table>
<div id="deleteDialog" style="padding:5px;width:400px;height:200px;display: none;">
	<p>确定要删除吗？</p>
</div>
<div id="userEditWindow" class="easyui-window"  closable="true" collapsible="false" minimizable="false"
	maximizable="false" modal="true" closed="true" style="width: 340px; height: 400px;">
	<iframe id="editIframe" style="height:99%;width:100%" frameborder="0"
		scrolling="auto">
	</iframe>
</div>
</body>
</html>